package com.edu.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import com.edu.entiy.User;
import com.edu.entiy.Book;
import com.edu.entiy.Problem;
import com.edu.service.ProblemService;
import com.edu.service.impl.ProblemServiceImpl;
import com.edu.utils.PageTool;
import com.edu.utils.PaginationUtils;

/**
 * Servlet implementation class ProblemServlet
 */
@WebServlet("/problemServlet/*")
public class ProblemServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ProblemService problemService = new ProblemServiceImpl();
    public ProblemServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    //分页
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//获取传入的数据
    	HttpSession session = request.getSession();
    	User user=(User)session.getAttribute("user");
    	
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        String word = request.getParameter("name");
        System.out.println(word);
        if(user.getStatus()==2) {
        	PageTool<Problem> pageTool = problemService.page(_currentPage, _pageSize,word,null);
        	String pageResult =  PaginationUtils.getPagation(pageTool.getTotalCount(), pageTool.getCurrentPage(), pageTool.getPageSize(), "/problemServlet/page");
        	request.setAttribute("pageResult", pageResult);
        	request.setAttribute("problemList", pageTool.getRows());
        	System.out.println(pageTool.getRows());
        	
        	
        	
        	request.getRequestDispatcher("/admin/admin_feedback.jsp").forward(request, response);
    	}else {
    		PageTool<Problem> pageTool = problemService.page(_currentPage, _pageSize,word,user.getId());
    		String pageResult =  PaginationUtils.getPagation(pageTool.getTotalCount(), pageTool.getCurrentPage(), pageTool.getPageSize(), "/problemServlet/page");
       	  	request.setAttribute("pageResult", pageResult);
       	  	request.setAttribute("problemList", pageTool.getRows());
       	  	System.out.println(pageTool.getRows());
       	  	request.getRequestDispatcher("/user/myproblem.jsp").forward(request, response);
		}
       
       
    }
    //添加
    public void addProblem(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException, ServletException, IOException {
		//获取参数
    	HttpSession session = request.getSession();
    	Problem problem = new Problem();
    	User user=(User)session.getAttribute("user");
    	problem.setAid(user.getId());
		BeanUtils.populate(problem, request.getParameterMap());
		int count = problemService.addProblem(problem);
		if(count == 1 ) {
			System.out.println("反馈成功");
			request.getRequestDispatcher("/user/result.jsp").forward(request, response);
		}else if(count == -1){
			System.out.println("反馈失败!");
		}else {
			System.out.println("反馈失败");
		}
	}
    //修改
    public void updateProblem(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, InvocationTargetException, ServletException, IOException {
		int pid=Integer.parseInt(request.getParameter("pid"));
		String status=request.getParameter("status");
		int count = problemService.updateProblem(pid, status);
		//对数据进行合法校验
		if(count == 1) {
			System.out.println("修改成功");
			request.getRequestDispatcher("/problemServlet/page").forward(request, response);
			
		}else {
			System.out.println("修改失败");
		}
	}
    public void deleteProblem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
    	int pid=Integer.parseInt(request.getParameter("Id"));
    	System.out.println(pid);
		int count = problemService.deleteProblem(pid);
		if(count == 1) {
			System.out.println("删除成功");
			request.getRequestDispatcher("/problemServlet/page").forward(request, response);
		}else {
			System.out.println("删除失败");
		}
	}
}









